package com.onlineshop.controller.admin;

import com.onlineshop.dao.ProductDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/admin/products/delete")
public class DeleteProductServlet extends HttpServlet {
    private ProductDao productDao = new ProductDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if (idStr == null || idStr.trim().isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/admin/products?error=无效请求");
            return;
        }

        try {
            int productId = Integer.parseInt(idStr);
            boolean success = productDao.deleteProduct(productId);
            if (success) {
                response.sendRedirect(request.getContextPath() + "/admin/products?success=删除商品成功");
            } else {
                response.sendRedirect(request.getContextPath() + "/admin/products?error=删除商品失败");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect(request.getContextPath() + "/admin/products?error=无效的商品ID");
        }
    }
}